package com.health.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.health.dao.SetMealMapper;
import com.health.pojo.Setmeal;
import com.health.vo.PageResult;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SetMealServiceImpl implements SetMealService{

    @Resource
    private  SetMealMapper setMealMapper;

    /**
     * 分页查询套餐
     */
    public PageResult<Setmeal> findPage(Integer pageNow, Integer pageSize, String query) {
        //分页插件
        PageHelper.startPage(pageNow,pageSize);
        //page中包含查询的总数量和列表
        Page<Setmeal> page= setMealMapper.findByQuery(query);

        return new PageResult<Setmeal>(page.getTotal(),page.getResult());
    }

    /**
     * 删除套餐
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteById(Integer id) {
        //查询order表中是否存在预订,存在则无法删除
        int count=setMealMapper.findCountInOrder(id);

        if (count>0){
            throw new RuntimeException("套餐被预订,无法删除");
        }

        //删除套餐与检查组的关联
        setMealMapper.deleteSetMealCheckGroup(id);
        //删除套餐
        return setMealMapper.deleteById(id);
    }
    /**
     * 添加套餐
     */
    @Transactional(rollbackFor = Exception.class)
    public int add(Setmeal setmeal, Integer[] checkGroupIds) {
        //添加套餐信息
        int count=setMealMapper.add(setmeal);

        //添加套餐关联的检查组,判断checkGroupIds是否为null且长度>0
        if (checkGroupIds !=null && checkGroupIds.length>0){
            Integer setmealId = setmeal.getId();
            setMealMapper.addCheckGroupIds(checkGroupIds,setmealId);
        }

        return count;
    }
    /**
     * 根据id查询套餐-数据回显
     */
    public Setmeal findById(Integer id) {

        return setMealMapper.findById(id);
    }
    /**
     * 根据id查询套餐关联的检查组id-数据回显
     */
    public List<Integer> findCheckGroupIds(Integer id) {

        return setMealMapper.findCheckGroupIds(id);
    }

    /**
     * 修改套餐信息
     */
    @Transactional(rollbackFor = Exception.class)
    public int update(Setmeal setmeal, Integer[] checkGroupIds) {
        //修改setmeal
        int count=setMealMapper.update(setmeal);

        //删除原来关联的检查组id
        Integer setmealId = setmeal.getId();
        setMealMapper.deleteSetMealCheckGroup(setmealId);
        //修改套餐关联的检查组id
        if (checkGroupIds !=null && checkGroupIds.length>0){
            setMealMapper.addCheckGroupIds(checkGroupIds,setmealId);
        }

        return count;
    }

    /**
     * 查询套餐图片文件名
     */
    public List<String> findImgs() {

        return setMealMapper.findImgs();
    }

    /**
     * 查询所有套餐
     */
    public List<Setmeal> findAll() {

        return setMealMapper.findAll();
    }
    /**
     * 查询套餐详情
     */
    public Setmeal findDetail(Integer id) {
        return setMealMapper.findDetail(id);
    }
    /**
     * 热门套餐数据报告
     * 预订数量前三的套餐
     */
    public List<Map<String, Object>> findSetMealReport() {
        return setMealMapper.findSetMealReport();
    }

    public List<Map<String, Object>> findSetMealCount(Date start, Date end) {
        return setMealMapper.findSetMealCount(start,end);
    }
}
